home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / random.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  24KB  |  762 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Random variable generators.
  5.  
  6.     integers
  7.     --------
  8.            uniform within range
  9.  
  10.     sequences
  11.     ---------
  12.            pick random element
  13.            pick random sample
  14.            generate random permutation
  15.  
  16.     distributions on the real line:
  17.     ------------------------------
  18.            uniform
  19.            normal (Gaussian)
  20.            lognormal
  21.            negative exponential
  22.            gamma
  23.            beta
  24.            pareto
  25.            Weibull
  26.  
  27.     distributions on the circle (angles 0 to 2pi)
  28.     ---------------------------------------------
  29.            circular uniform
  30.            von Mises
  31.  
  32. General notes on the underlying Mersenne Twister core generator:
  33.  
  34. * The period is 2**19937-1.
  35. * It is one of the most extensively tested generators in existence.
  36. * Without a direct way to compute N steps forward, the semantics of
  37.   jumpahead(n) are weakened to simply jump to another distant state and rely
  38.   on the large period to avoid overlapping sequences.
  39. * The random() method is implemented in C, executes in a single Python step,
  40.   and is, therefore, threadsafe.
  41.  
  42. '''
  43. from warnings import warn as _warn
  44. from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
  45. from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
  46. from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
  47. from os import urandom as _urandom
  48. from binascii import hexlify as _hexlify
  49. __all__ = [
  50.     'Random',
  51.     'seed',
  52.     'random',
  53.     'uniform',
  54.     'randint',
  55.     'choice',
  56.     'sample',
  57.     'randrange',
  58.     'shuffle',
  59.     'normalvariate',
  60.     'lognormvariate',
  61.     'expovariate',
  62.     'vonmisesvariate',
  63.     'gammavariate',
  64.     'gauss',
  65.     'betavariate',
  66.     'paretovariate',
  67.     'weibullvariate',
  68.     'getstate',
  69.     'setstate',
  70.     'jumpahead',
  71.     'WichmannHill',
  72.     'getrandbits',
  73.     'SystemRandom']
  74. NV_MAGICCONST = 4 * _exp(-0.5) / _sqrt(2)
  75. TWOPI = 2 * _pi
  76. LOG4 = _log(4)
  77. SG_MAGICCONST = 1 + _log(4.5)
  78. BPF = 53
  79. RECIP_BPF = 2 ** (-BPF)
  80. import _random
  81.  
  82. class Random(_random.Random):
  83.     """Random number generator base class used by bound module functions.
  84.  
  85.     Used to instantiate instances of Random to get generators that don't
  86.     share state.  Especially useful for multi-threaded programs, creating
  87.     a different instance of Random for each thread, and using the jumpahead()
  88.     method to ensure that the generated sequences seen by each thread don't
  89.     overlap.
  90.  
  91.     Class Random can also be subclassed if you want to use a different basic
  92.     generator of your own devising: in that case, override the following
  93.     methods:  random(), seed(), getstate(), setstate() and jumpahead().
  94.     Optionally, implement a getrandombits() method so that randrange()
  95.     can cover arbitrarily large ranges.
  96.  
  97.     """
  98.     VERSION = 2
  99.     
  100.     def __init__(self, x = None):
  101.         '''Initialize an instance.
  102.  
  103.         Optional argument x controls seeding, as for Random.seed().
  104.         '''
  105.         self.seed(x)
  106.         self.gauss_next = None
  107.  
  108.     
  109.     def seed(self, a = None):
  110.         '''Initialize internal state from hashable object.
  111.  
  112.         None or no argument seeds from current time or from an operating
  113.         system specific randomness source if available.
  114.  
  115.         If a is not None or an int or long, hash(a) is used instead.
  116.         '''
  117.         if a is None:
  118.             
  119.             try:
  120.                 a = long(_hexlify(_urandom(16)), 16)
  121.             except NotImplementedError:
  122.                 import time as time
  123.                 a = long(time.time() * 256)
  124.             except:
  125.                 None<EXCEPTION MATCH>NotImplementedError
  126.             
  127.  
  128.         None<EXCEPTION MATCH>NotImplementedError
  129.         super(Random, self).seed(a)
  130.         self.gauss_next = None
  131.  
  132.     
  133.     def getstate(self):
  134.         '''Return internal state; can be passed to setstate() later.'''
  135.         return (self.VERSION, super(Random, self).getstate(), self.gauss_next)
  136.  
  137.     
  138.     def setstate(self, state):
  139.         '''Restore internal state from object returned by getstate().'''
  140.         version = state[0]
  141.         if version == 2:
  142.             (version, internalstate, self.gauss_next) = state
  143.             super(Random, self).setstate(internalstate)
  144.         else:
  145.             raise ValueError('state with version %s passed to Random.setstate() of version %s' % (version, self.VERSION))
  146.  
  147.     
  148.     def __getstate__(self):
  149.         return self.getstate()
  150.  
  151.     
  152.     def __setstate__(self, state):
  153.         self.setstate(state)
  154.  
  155.     
  156.     def __reduce__(self):
  157.         return (self.__class__, (), self.getstate())
  158.  
  159.     
  160.     def randrange(self, start, stop = None, step = 1, int = int, default = None, maxwidth = 0x1L << BPF):
  161.         """Choose a random item from range(start, stop[, step]).
  162.  
  163.         This fixes the problem with randint() which includes the
  164.         endpoint; in Python this is usually not what you want.
  165.         Do not supply the 'int', 'default', and 'maxwidth' arguments.
  166.         """
  167.         istart = int(start)
  168.         if istart != start:
  169.             raise ValueError, 'non-integer arg 1 for randrange()'
  170.         
  171.         if stop is default:
  172.             if istart > 0:
  173.                 if istart >= maxwidth:
  174.                     return self._randbelow(istart)
  175.                 
  176.                 return int(self.random() * istart)
  177.             
  178.             raise ValueError, 'empty range for randrange()'
  179.         
  180.         istop = int(stop)
  181.         if istop != stop:
  182.             raise ValueError, 'non-integer stop for randrange()'
  183.         
  184.         width = istop - istart
  185.         if step == 1 and width > 0:
  186.             if width >= maxwidth:
  187.                 return int(istart + self._randbelow(width))
  188.             
  189.             return int(istart + int(self.random() * width))
  190.         
  191.         if step == 1:
  192.             raise ValueError, 'empty range for randrange() (%d,%d, %d)' % (istart, istop, width)
  193.         
  194.         istep = int(step)
  195.         if istep != step:
  196.             raise ValueError, 'non-integer step for randrange()'
  197.         
  198.         if istep > 0:
  199.             n = (width + istep - 1) // istep
  200.         elif istep < 0:
  201.             n = (width + istep + 1) // istep
  202.         else:
  203.             raise ValueError, 'zero step for randrange()'
  204.         if n <= 0:
  205.             raise ValueError, 'empty range for randrange()'
  206.         
  207.         if n >= maxwidth:
  208.             return istart + istep * self._randbelow(n)
  209.         
  210.         return istart + istep * int(self.random() * n)
  211.  
  212.     
  213.     def randint(self, a, b):
  214.         '''Return random integer in range [a, b], including both end points.
  215.         '''
  216.         return self.randrange(a, b + 1)
  217.  
  218.     
  219.     def _randbelow(self, n, _log = _log, int = int, _maxwidth = 0x1L << BPF, _Method = _MethodType, _BuiltinMethod = _BuiltinMethodType):
  220.         '''Return a random int in the range [0,n)
  221.  
  222.         Handles the case where n has more bits than returned
  223.         by a single call to the underlying generator.
  224.         '''
  225.         
  226.         try:
  227.             getrandbits = self.getrandbits
  228.         except AttributeError:
  229.             pass
  230.  
  231.         if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
  232.             k = int(1.00001 + _log(n - 1, 2))
  233.             r = getrandbits(k)
  234.             while r >= n:
  235.                 r = getrandbits(k)
  236.             return r
  237.         
  238.         if n >= _maxwidth:
  239.             _warn('Underlying random() generator does not supply \nenough bits to choose from a population range this large')
  240.         
  241.         return int(self.random() * n)
  242.  
  243.     
  244.     def choice(self, seq):
  245.         '''Choose a random element from a non-empty sequence.'''
  246.         return seq[int(self.random() * len(seq))]
  247.  
  248.     
  249.     def shuffle(self, x, random = None, int = int):
  250.         '''x, random=random.random -> shuffle list x in place; return None.
  251.  
  252.         Optional arg random is a 0-argument function returning a random
  253.         float in [0.0, 1.0); by default, the standard random.random.
  254.         '''
  255.         if random is None:
  256.             random = self.random
  257.         
  258.         for i in reversed(xrange(1, len(x))):
  259.             j = int(random() * (i + 1))
  260.             x[i] = x[j]
  261.             x[j] = x[i]
  262.         
  263.  
  264.     
  265.     def sample(self, population, k):
  266.         '''Chooses k unique random elements from a population sequence.
  267.  
  268.         Returns a new list containing elements from the population while
  269.         leaving the original population unchanged.  The resulting list is
  270.         in selection order so that all sub-slices will also be valid random
  271.         samples.  This allows raffle winners (the sample) to be partitioned
  272.         into grand prize and second place winners (the subslices).
  273.  
  274.         Members of the population need not be hashable or unique.  If the
  275.         population contains repeats, then each occurrence is a possible
  276.         selection in the sample.
  277.  
  278.         To choose a sample in a range of integers, use xrange as an argument.
  279.         This is especially fast and space efficient for sampling from a
  280.         large population:   sample(xrange(10000000), 60)
  281.         '''
  282.         n = len(population)
  283.         if k <= k:
  284.             pass
  285.         elif not k <= n:
  286.             raise ValueError, 'sample larger than population'
  287.         
  288.         random = self.random
  289.         _int = int
  290.         result = [
  291.             None] * k
  292.         setsize = 21
  293.         if k > 5:
  294.             setsize += 4 ** _ceil(_log(k * 3, 4))
  295.         
  296.         if n <= setsize or hasattr(population, 'keys'):
  297.             pool = list(population)
  298.             for i in xrange(k):
  299.                 j = _int(random() * (n - i))
  300.                 result[i] = pool[j]
  301.                 pool[j] = pool[n - i - 1]
  302.             
  303.         else:
  304.             
  305.             try:
  306.                 selected = set()
  307.                 selected_add = selected.add
  308.                 for i in xrange(k):
  309.                     j = _int(random() * n)
  310.                     while j in selected:
  311.                         j = _int(random() * n)
  312.                     selected_add(j)
  313.                     result[i] = population[j]
  314.             except (TypeError, KeyError):
  315.                 if isinstance(population, list):
  316.                     raise 
  317.                 
  318.                 return self.sample(tuple(population), k)
  319.  
  320.         return result
  321.  
  322.     
  323.     def uniform(self, a, b):
  324.         '''Get a random number in the range [a, b).'''
  325.         return a + (b - a) * self.random()
  326.  
  327.     
  328.     def normalvariate(self, mu, sigma):
  329.         '''Normal distribution.
  330.  
  331.         mu is the mean, and sigma is the standard deviation.
  332.  
  333.         '''
  334.         random = self.random
  335.         while None:
  336.             u1 = random()
  337.             u2 = 1 - random()
  338.             z = NV_MAGICCONST * (u1 - 0.5) / u2
  339.             zz = z * z / 4
  340.             if zz <= -_log(u2):
  341.                 break
  342.                 continue
  343.             continue
  344.             return mu + z * sigma
  345.  
  346.     
  347.     def lognormvariate(self, mu, sigma):
  348.         """Log normal distribution.
  349.  
  350.         If you take the natural logarithm of this distribution, you'll get a
  351.         normal distribution with mean mu and standard deviation sigma.
  352.         mu can have any value, and sigma must be greater than zero.
  353.  
  354.         """
  355.         return _exp(self.normalvariate(mu, sigma))
  356.  
  357.     
  358.     def expovariate(self, lambd):
  359.         '''Exponential distribution.
  360.  
  361.         lambd is 1.0 divided by the desired mean.  (The parameter would be
  362.         called "lambda", but that is a reserved word in Python.)  Returned
  363.         values range from 0 to positive infinity.
  364.  
  365.         '''
  366.         random = self.random
  367.         u = random()
  368.         while u <= 1e-07:
  369.             u = random()
  370.         return -_log(u) / lambd
  371.  
  372.     
  373.     def vonmisesvariate(self, mu, kappa):
  374.         '''Circular data distribution.
  375.  
  376.         mu is the mean angle, expressed in radians between 0 and 2*pi, and
  377.         kappa is the concentration parameter, which must be greater than or
  378.         equal to zero.  If kappa is equal to zero, this distribution reduces
  379.         to a uniform random angle over the range 0 to 2*pi.
  380.  
  381.         '''
  382.         random = self.random
  383.         if kappa <= 1e-06:
  384.             return TWOPI * random()
  385.         
  386.         a = 1 + _sqrt(1 + 4 * kappa * kappa)
  387.         b = (a - _sqrt(2 * a)) / 2 * kappa
  388.         r = (1 + b * b) / 2 * b
  389.         while None:
  390.             u1 = random()
  391.             z = _cos(_pi * u1)
  392.             f = (1 + r * z) / (r + z)
  393.             c = kappa * (r - f)
  394.             u2 = random()
  395.             if u2 < c * (2 - c) or u2 <= c * _exp(1 - c):
  396.                 break
  397.                 continue
  398.             continue
  399.             u3 = random()
  400.             if u3 > 0.5:
  401.                 theta = mu % TWOPI + _acos(f)
  402.             else:
  403.                 theta = mu % TWOPI - _acos(f)
  404.         return theta
  405.  
  406.     
  407.     def gammavariate(self, alpha, beta):
  408.         '''Gamma distribution.  Not the gamma function!
  409.  
  410.         Conditions on the parameters are alpha > 0 and beta > 0.
  411.  
  412.         '''
  413.         if alpha <= 0 or beta <= 0:
  414.             raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
  415.         
  416.         random = self.random
  417.         if alpha > 1:
  418.             ainv = _sqrt(2 * alpha - 1)
  419.             bbb = alpha - LOG4
  420.             ccc = alpha + ainv
  421.             while None:
  422.                 u1 = random()
  423.                 if u1 < u1:
  424.                     pass
  425.                 elif not u1 < 1:
  426.                     continue
  427.                 
  428.                 u2 = 1 - random()
  429.                 v = _log(u1 / (1 - u1)) / ainv
  430.                 x = alpha * _exp(v)
  431.                 z = u1 * u1 * u2
  432.                 r = bbb + ccc * v - x
  433.                 if r + SG_MAGICCONST - 4.5 * z >= 0 or r >= _log(z):
  434.                     return x * beta
  435.                     continue
  436.                 continue
  437.         alpha > 1
  438.         if alpha == 1:
  439.             u = random()
  440.             while u <= 1e-07:
  441.                 u = random()
  442.             return -_log(u) * beta
  443.         else:
  444.             while None:
  445.                 u = random()
  446.                 b = (_e + alpha) / _e
  447.                 p = b * u
  448.                 if p <= 1:
  449.                     x = p ** (1 / alpha)
  450.                 else:
  451.                     x = -_log((b - p) / alpha)
  452.                 u1 = random()
  453.                 if p > 1:
  454.                     if u1 <= x ** (alpha - 1):
  455.                         break
  456.                     
  457.                 if u1 <= _exp(-x):
  458.                     break
  459.                     continue
  460.                 continue
  461.                 return x * beta
  462.                 return None
  463.  
  464.     
  465.     def gauss(self, mu, sigma):
  466.         '''Gaussian distribution.
  467.  
  468.         mu is the mean, and sigma is the standard deviation.  This is
  469.         slightly faster than the normalvariate() function.
  470.  
  471.         Not thread-safe without a lock around calls.
  472.  
  473.         '''
  474.         random = self.random
  475.         z = self.gauss_next
  476.         self.gauss_next = None
  477.         if z is None:
  478.             x2pi = random() * TWOPI
  479.             g2rad = _sqrt(-2 * _log(1 - random()))
  480.             z = _cos(x2pi) * g2rad
  481.             self.gauss_next = _sin(x2pi) * g2rad
  482.         
  483.         return mu + z * sigma
  484.  
  485.     
  486.     def betavariate(self, alpha, beta):
  487.         '''Beta distribution.
  488.  
  489.         Conditions on the parameters are alpha > 0 and beta > 0.
  490.         Returned values range between 0 and 1.
  491.  
  492.         '''
  493.         y = self.gammavariate(alpha, 1)
  494.         if y == 0:
  495.             return 0
  496.         else:
  497.             return y / (y + self.gammavariate(beta, 1))
  498.  
  499.     
  500.     def paretovariate(self, alpha):
  501.         '''Pareto distribution.  alpha is the shape parameter.'''
  502.         u = 1 - self.random()
  503.         return 1 / pow(u, 1 / alpha)
  504.  
  505.     
  506.     def weibullvariate(self, alpha, beta):
  507.         '''Weibull distribution.
  508.  
  509.         alpha is the scale parameter and beta is the shape parameter.
  510.  
  511.         '''
  512.         u = 1 - self.random()
  513.         return alpha * pow(-_log(u), 1 / beta)
  514.  
  515.  
  516.  
  517. class WichmannHill(Random):
  518.     VERSION = 1
  519.     
  520.     def seed(self, a = None):
  521.         '''Initialize internal state from hashable object.
  522.  
  523.         None or no argument seeds from current time or from an operating
  524.         system specific randomness source if available.
  525.  
  526.         If a is not None or an int or long, hash(a) is used instead.
  527.  
  528.         If a is an int or long, a is used directly.  Distinct values between
  529.         0 and 27814431486575L inclusive are guaranteed to yield distinct
  530.         internal states (this guarantee is specific to the default
  531.         Wichmann-Hill generator).
  532.         '''
  533.         if a is None:
  534.             
  535.             try:
  536.                 a = long(_hexlify(_urandom(16)), 16)
  537.             except NotImplementedError:
  538.                 import time
  539.                 a = long(time.time() * 256)
  540.             except:
  541.                 None<EXCEPTION MATCH>NotImplementedError
  542.             
  543.  
  544.         None<EXCEPTION MATCH>NotImplementedError
  545.         if not isinstance(a, (int, long)):
  546.             a = hash(a)
  547.         
  548.         (a, x) = divmod(a, 30268)
  549.         (a, y) = divmod(a, 30306)
  550.         (a, z) = divmod(a, 30322)
  551.         self._seed = (int(x) + 1, int(y) + 1, int(z) + 1)
  552.         self.gauss_next = None
  553.  
  554.     
  555.     def random(self):
  556.         '''Get the next random number in the range [0.0, 1.0).'''
  557.         (x, y, z) = self._seed
  558.         x = 171 * x % 30269
  559.         y = 172 * y % 30307
  560.         z = 170 * z % 30323
  561.         self._seed = (x, y, z)
  562.         return (x / 30269 + y / 30307 + z / 30323) % 1
  563.  
  564.     
  565.     def getstate(self):
  566.         '''Return internal state; can be passed to setstate() later.'''
  567.         return (self.VERSION, self._seed, self.gauss_next)
  568.  
  569.     
  570.     def setstate(self, state):
  571.         '''Restore internal state from object returned by getstate().'''
  572.         version = state[0]
  573.         if version == 1:
  574.             (version, self._seed, self.gauss_next) = state
  575.         else:
  576.             raise ValueError('state with version %s passed to Random.setstate() of version %s' % (version, self.VERSION))
  577.  
  578.     
  579.     def jumpahead(self, n):
  580.         '''Act as if n calls to random() were made, but quickly.
  581.  
  582.         n is an int, greater than or equal to 0.
  583.  
  584.         Example use:  If you have 2 threads and know that each will
  585.         consume no more than a million random numbers, create two Random
  586.         objects r1 and r2, then do
  587.             r2.setstate(r1.getstate())
  588.             r2.jumpahead(1000000)
  589.         Then r1 and r2 will use guaranteed-disjoint segments of the full
  590.         period.
  591.         '''
  592.         if not n >= 0:
  593.             raise ValueError('n must be >= 0')
  594.         
  595.         (x, y, z) = self._seed
  596.         x = int(x * pow(171, n, 30269)) % 30269
  597.         y = int(y * pow(172, n, 30307)) % 30307
  598.         z = int(z * pow(170, n, 30323)) % 30323
  599.         self._seed = (x, y, z)
  600.  
  601.     
  602.     def __whseed(self, x = 0, y = 0, z = 0):
  603.         '''Set the Wichmann-Hill seed from (x, y, z).
  604.  
  605.         These must be integers in the range [0, 256).
  606.         '''
  607.         if type(y) == type(y) and type(z) == type(z):
  608.             pass
  609.         elif not type(z) == int:
  610.             raise TypeError('seeds must be integers')
  611.         
  612.         if x == x and y == y:
  613.             pass
  614.         elif y == z:
  615.             import time
  616.             t = long(time.time() * 256)
  617.             t = int(t & 16777215 ^ t >> 24)
  618.             (t, x) = divmod(t, 256)
  619.             (t, y) = divmod(t, 256)
  620.             (t, z) = divmod(t, 256)
  621.         
  622.         if not z:
  623.             pass
  624.         self._seed = (None, 1 if not x else 1, 1)
  625.         self.gauss_next = None
  626.  
  627.     
  628.     def whseed(self, a = None):
  629.         """Seed from hashable object's hash code.
  630.  
  631.         None or no argument seeds from current time.  It is not guaranteed
  632.         that objects with distinct hash codes lead to distinct internal
  633.         states.
  634.  
  635.         This is obsolete, provided for compatibility with the seed routine
  636.         used prior to Python 2.1.  Use the .seed() method instead.
  637.         """
  638.         if a is None:
  639.             self._WichmannHill__whseed()
  640.             return None
  641.         
  642.         a = hash(a)
  643.         (a, x) = divmod(a, 256)
  644.         (a, y) = divmod(a, 256)
  645.         (a, z) = divmod(a, 256)
  646.         if not (x + a) % 256:
  647.             pass
  648.         x = 1
  649.         if not (y + a) % 256:
  650.             pass
  651.         y = 1
  652.         if not (z + a) % 256:
  653.             pass
  654.         z = 1
  655.         self._WichmannHill__whseed(x, y, z)
  656.  
  657.  
  658.  
  659. class SystemRandom(Random):
  660.     '''Alternate random number generator using sources provided
  661.     by the operating system (such as /dev/urandom on Unix or
  662.     CryptGenRandom on Windows).
  663.  
  664.      Not available on all systems (see os.urandom() for details).
  665.     '''
  666.     
  667.     def random(self):
  668.         '''Get the next random number in the range [0.0, 1.0).'''
  669.         return (long(_hexlify(_urandom(7)), 16) >> 3) * RECIP_BPF
  670.  
  671.     
  672.     def getrandbits(self, k):
  673.         '''getrandbits(k) -> x.  Generates a long int with k random bits.'''
  674.         if k <= 0:
  675.             raise ValueError('number of bits must be greater than zero')
  676.         
  677.         if k != int(k):
  678.             raise TypeError('number of bits should be an integer')
  679.         
  680.         bytes = (k + 7) // 8
  681.         x = long(_hexlify(_urandom(bytes)), 16)
  682.         return x >> bytes * 8 - k
  683.  
  684.     
  685.     def _stub(self, *args, **kwds):
  686.         '''Stub method.  Not used for a system random number generator.'''
  687.         pass
  688.  
  689.     seed = jumpahead = _stub
  690.     
  691.     def _notimplemented(self, *args, **kwds):
  692.         '''Method should not be called for a system random number generator.'''
  693.         raise NotImplementedError('System entropy source does not have state.')
  694.  
  695.     getstate = setstate = _notimplemented
  696.  
  697.  
  698. def _test_generator(n, func, args):
  699.     import time
  700.     print n, 'times', func.__name__
  701.     total = 0
  702.     sqsum = 0
  703.     smallest = 1e+10
  704.     largest = -1e+10
  705.     t0 = time.time()
  706.     for i in range(n):
  707.         x = func(*args)
  708.         total += x
  709.         sqsum = sqsum + x * x
  710.         smallest = min(x, smallest)
  711.         largest = max(x, largest)
  712.     
  713.     t1 = time.time()
  714.     print round(t1 - t0, 3), 'sec,',
  715.     avg = total / n
  716.     stddev = _sqrt(sqsum / n - avg * avg)
  717.     print 'avg %g, stddev %g, min %g, max %g' % (avg, stddev, smallest, largest)
  718.  
  719.  
  720. def _test(N = 2000):
  721.     _test_generator(N, random, ())
  722.     _test_generator(N, normalvariate, (0, 1))
  723.     _test_generator(N, lognormvariate, (0, 1))
  724.     _test_generator(N, vonmisesvariate, (0, 1))
  725.     _test_generator(N, gammavariate, (0.01, 1))
  726.     _test_generator(N, gammavariate, (0.1, 1))
  727.     _test_generator(N, gammavariate, (0.1, 2))
  728.     _test_generator(N, gammavariate, (0.5, 1))
  729.     _test_generator(N, gammavariate, (0.9, 1))
  730.     _test_generator(N, gammavariate, (1, 1))
  731.     _test_generator(N, gammavariate, (2, 1))
  732.     _test_generator(N, gammavariate, (20, 1))
  733.     _test_generator(N, gammavariate, (200, 1))
  734.     _test_generator(N, gauss, (0, 1))
  735.     _test_generator(N, betavariate, (3, 3))
  736.  
  737. _inst = Random()
  738. seed = _inst.seed
  739. random = _inst.random
  740. uniform = _inst.uniform
  741. randint = _inst.randint
  742. choice = _inst.choice
  743. randrange = _inst.randrange
  744. sample = _inst.sample
  745. shuffle = _inst.shuffle
  746. normalvariate = _inst.normalvariate
  747. lognormvariate = _inst.lognormvariate
  748. expovariate = _inst.expovariate
  749. vonmisesvariate = _inst.vonmisesvariate
  750. gammavariate = _inst.gammavariate
  751. gauss = _inst.gauss
  752. betavariate = _inst.betavariate
  753. paretovariate = _inst.paretovariate
  754. weibullvariate = _inst.weibullvariate
  755. getstate = _inst.getstate
  756. setstate = _inst.setstate
  757. jumpahead = _inst.jumpahead
  758. getrandbits = _inst.getrandbits
  759. if __name__ == '__main__':
  760.     _test()
  761.  
  762.